//
//  CTPopMenuManager.m
//  IBDP
//
//  Created by cattsoft on 2017/7/14.
//  Copyright © 2017年 cattsoft. All rights reserved.
//

#import "CTPopMenuManager.h"

@interface CTPopMenuManager()

@property (nonatomic,strong) CTPopMenuView *popmenuView;

@end

@implementation CTPopMenuManager

+ (instancetype)sharedManager {
    static CTPopMenuManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[CTPopMenuManager alloc] init];
    });
    return manager;
}

// 控制宽高的（写死）
+ (void)showPopMenuWithView:(UIView *)view itemWidth:(CGFloat)width itemHeight:(CGFloat)height items:(NSArray *)itemArr action:(action)action dismissAutomatically:(BOOL)dismissed {
    [[self sharedManager] showPopMenuWithView:view itemWidth:width itemHeight:height items:itemArr action:action dismissedAutomatically:dismissed];
}

+ (void)setBackgroundColor:(UIColor *)color{
    [[self sharedManager] setBackgroundColour:color];
}

+ (void)setBackgroundView:(UIView *)view{
    [[self sharedManager] setBackView:view];
}

+ (void)dismiss{
    [[self sharedManager] dismiss];
}

#pragma mark - implementation
- (void)showPopMenuWithView:(UIView *)view itemWidth:(CGFloat)width itemHeight:(CGFloat)height items:(NSArray *)itemArr{
    if (self.popmenuView) {
        [self dismiss];
    }
    self.popmenuView = [[CTPopMenuView alloc] initWithView:view itemWidth:width itemHeight:height items:itemArr style:self.menuStyle];
    self.popmenuView.frame = [UIScreen mainScreen].bounds;
    [[UIApplication sharedApplication].keyWindow addSubview:self.popmenuView];
    [UIView animateWithDuration:0.25f animations:^{
        self.popmenuView.tableView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    }];
}

// 第二种
- (void)showPopMenuWithView:(UIView *)view itemWidth:(CGFloat)width itemHeight:(CGFloat)height items:(NSArray *)itemArr action:(action)action dismissedAutomatically:(BOOL)dismissed{
    [self showPopMenuWithView:view itemWidth:width itemHeight:height items:itemArr];
    self.popmenuView.dismissed = dismissed;
    if (action) {
        self.popmenuView.clickAction = action;
    }
}

- (void)setBackgroundColour:(UIColor *)color{
    if (self.popmenuView) {
        self.popmenuView.tableView.backgroundColor = color;
    }
}

- (void)setBackView:(UIView *)view{
    if (self.popmenuView) {
        self.popmenuView.tableView.backgroundView = view;
    }
}

- (void)dismiss{
    if (self.dismisseAction) {
        self.dismisseAction();
    }
    [UIView animateWithDuration:0.2f animations:^{
        self.popmenuView.tableView.transform = CGAffineTransformMakeScale(0.001, 0.001);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.popmenuView.triangleLayer removeFromSuperlayer];
        });
    } completion:^(BOOL finished) {
        [self.popmenuView.tableView removeFromSuperview];
        [self.popmenuView removeFromSuperview];
        self.popmenuView = nil;
        self.menuStyle = nil;
    }];
}

- (CTPopMenuStyle *)menuStyle {
    if (!_menuStyle) {
        _menuStyle = [[CTPopMenuStyle alloc] init];
    }
    return _menuStyle;
}

@end
